<?php

/*
 * This file will be used for the pagination of the search results.
 * 
 * Created by: Peter Agno Jr.
 * Date created: November 18, 2011
 * 
 * In parameters: none
 * Out parameters: none
 */
?>

    // START - JQUERY FOR PAGINATION
    jQuery('.pageButton').live("click",function() {
        buttonId = this.id;

        tempSubstr = buttonId.split('_');
        totalPageNum = jQuery("#totalPageNum").val();
        viewPageNumLimit = jQuery("#viewPageNumLimit").val();

        // HIDE ALL ROWS IN THE SEARCH RESULTS ASCENDING
        for ( i = 1 ; i < tempSubstr[1] ; i++ ) {
            jQuery(".rowPage_"+i).hide();
        }

        // HIDE ALL ROWS IN THE SEARCH RESULTS DESCENDING
        for ( i = totalPageNum ; i > tempSubstr[1] ; i-- ) {
            jQuery(".rowPage_"+i).hide();
        }

        // SHOW THE SELECTED ROWS
        jQuery("."+buttonId).show();

        // SHOW ONLY LIMITED NUMBERS OF PAGE, DEPENDING ON THE viewPageNumLimit
        if ( totalPageNum > viewPageNumLimit ) {
            // DECREMENT AND SET THE prevPageButton
            prevPage = parseInt(parseFloat(tempSubstr[1])) - 1;
            jQuery(".prevPageButton").attr("id", "rowPage_"+prevPage);

            // INCREMENT AND SET THE nextPageButton
            nextPage = parseInt(parseFloat(tempSubstr[1])) + 1;
            jQuery(".nextPageButton").attr("id", "rowPage_"+nextPage);

            // HIDE ALL PAGINATION BUTTONS
            jQuery(".pageButton").hide();

            // SHOW PAGINATION NUMBERS WITHIN viewPageNumLimit
            for ( i = parseInt( tempSubstr[1] ) - ( parseInt( viewPageNumLimit / 2 ) ), viewCount = 1 ; i <= totalPageNum && viewCount <= viewPageNumLimit ; i++, viewCount++ ) {
                jQuery(".buttonNum_"+i).show();
            }

            // SHOW SPECIAL PAGINATION BUTTONS
            jQuery(".firstPageButton").show();
            jQuery(".prevPageButton").show();
            jQuery(".nextPageButton").show();
            jQuery(".lastPageButton").show();

            // HIDE nextPageButton AND lastPageButton IF THE LAST PAGE OR FINAL NUMBER WAS CLICKED
            if ( tempSubstr[1] == totalPageNum ) {
                jQuery(".nextPageButton").hide();
                jQuery(".lastPageButton").hide();
            }

            // HIDE firstPageButton AND prevPageButton IF THE FIRST PAGE OR PAGE 1 WAS CLICKED
            if ( tempSubstr[1] == 1 ) {           
                jQuery(".prevPageButton").hide();
                jQuery(".firstPageButton").hide();
            }


        }

        // UPDATE THE PREVIOUS PAGE BUTTON
        // jQuery(".pageButton").css({ 'color': 'blue' });
        jQuery(".pageButton").removeClass("paginationCurrentPage").addClass("paginationPageNumbers");
        
        // COLOR THE CLICKED PAGINATION NUMBER
        // jQuery(".buttonNum_"+tempSubstr[1]).css({ 'color': 'red' });
        jQuery(".buttonNum_"+tempSubstr[1]).removeClass("paginationPageNumbers").addClass("paginationCurrentPage");
    }); // END - JQUERY FOR PAGINATION